package com.example.asus.yuejian_10.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.bean.UserGuanzhu;
import com.example.asus.yuejian_10.imple.Home_actimple;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/11 0011.
 */

public class UserGuanZhuAdapter extends Adapter {

    private List<UserGuanzhu> userGuanzhus;
    private Context context;
    private LayoutInflater layoutInflater;
    public UserGuanZhuAdapter(Context context,List<UserGuanzhu> userGuanzhus){
        this.context=context;
        this.userGuanzhus=userGuanzhus;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView=LayoutInflater.from(context).inflate(R.layout.item_user_guanzhu,viewGroup,false);
        UserGuanZhuViewHolder holder=new UserGuanZhuViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        UserGuanZhuViewHolder userGuanZhuViewHolder= (UserGuanZhuViewHolder) viewHolder;
        final UserGuanzhu guanzhu=userGuanzhus.get(position);
        final String guanzhu_name=guanzhu.getName
                ();
        final String guanzhu_pic= Config.PIC_URL+guanzhu.getThumb();
        userGuanZhuViewHolder.guanzhu_name.setText(guanzhu_name);
        Picasso.with(context).load(guanzhu_pic).into(userGuanZhuViewHolder.guanzhu_pic);


        userGuanZhuViewHolder.guanzhu_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory
                        .create()).baseUrl(Config.URL).build();
                Home_actimple services = retrofit.create(Home_actimple.class);
                Call<Integer> call=services.getCClubState(guanzhu.getFan_id(),guanzhu.getUserid(),guanzhu.getFans_type());
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if (response.body()==0){
                            Toast.makeText(context,"取消失败",Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(context,"取消成功",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });


       /* userGuanZhuViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (guanzhu.getFans_type().equals("2")){
              *//*  Intent intent=new Intent(context, ClubActivity.class);
                intent.putExtra("Club_Id",guanzhu.getFan_id());
                intent.putExtra("Club_Name",guanzhu_name);
                intent.putExtra("Club_Pic",guanzhu_pic);
                context.startActivity(intent);
                }else
                {*//*
                    Intent intent=new Intent(context, TeaDetailActivity.class);
                    intent.putExtra("tea_id",guanzhu.getFan_id());
                    context.startActivity(intent);
                }
            }
        });*/

    }

    @Override
    public int getItemCount() {
        return userGuanzhus.size();
    }


    public class UserGuanZhuViewHolder extends RecyclerView.ViewHolder{


        public TextView guanzhu_name;
        public ImageView guanzhu_pic;
        public Button guanzhu_button;
        public View itemView;

        public UserGuanZhuViewHolder(View itemView) {
            super(itemView);
            this.itemView=itemView;
            guanzhu_name= (TextView) itemView.findViewById(R.id.guanzhu_name);
            guanzhu_pic= (ImageView) itemView.findViewById(R.id.guanzhu_pic);
            guanzhu_button= (Button) itemView.findViewById(R.id.guanzhu_button);
        }
    }
}
